<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属网点" prop="bankId">
        <el-input
          v-model="queryParams.bankId"
          placeholder="请输入所属网点id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="款箱编号" prop="boxId">
        <el-input
          v-model="queryParams.boxId"
          placeholder="请输入款箱编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱号" prop="boxNo">
        <el-input
          v-model="queryParams.boxNo"
          placeholder="请输入箱号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="款箱状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择款箱状态" clearable>
          <el-option
            v-for="dict in box_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="条码(含二维码)状态" prop="qrcodeStatus" label-width="150px">
        <el-select v-model="queryParams.qrcodeStatus" placeholder="请选择条码" clearable>
          <el-option
            v-for="dict in barcode_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="款箱类型" prop="boxTypeId">
        <el-input
          v-model="queryParams.boxTypeId"
          placeholder="请输入款箱类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构id" prop="orgId">
        <el-input
          v-model="queryParams.orgId"
          placeholder="请输入机构id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条码状态" prop="barcodeStatus">
        <el-select v-model="queryParams.barcodeStatus" placeholder="请选择条码状态" clearable>
          <el-option
            v-for="dict in barcode_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 285px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['service:boxInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:boxInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:boxInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['service:boxInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="boxInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="" align="center"  width="50" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="款箱编号" align="center" prop="boxId" />
      <el-table-column label="箱号" align="center" prop="boxNo" />
      <el-table-column label="款箱状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="box_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="boxBrand" />
      <el-table-column label="所属网点id" align="center" prop="bankId" width="150px" />
      <el-table-column label="清分地" align="center" prop="clearingPos" />
      <el-table-column label="当前位置" align="center" prop="currentPos" />
      <el-table-column label="出金库时间" align="center" prop="outOfVaultTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.outOfVaultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进网点时间" align="center" prop="inBankTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inBankTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出网点时间" align="center" prop="outOfBankTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.outOfBankTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入金库时间" align="center" prop="inVaultTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inVaultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="款箱类型" align="center" prop="boxTypeId" width="150px">
        <template #default="scope">
          <span>{{ getBoxTypeName(scope.row.boxTypeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属机构id" align="center" prop="orgId" width="150px"/>
      <el-table-column label="是否转换" align="center" prop="isTransfer" />
      <el-table-column label="条码状态" align="center" prop="barcodeStatus">
        <template #default="scope">
          <dict-tag :options="barcode_status" :value="scope.row.barcodeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="条码(含二维码)状态" align="center" prop="qrcodeStatus" width="150px">
        <template #default="scope">
          <dict-tag :options="barcode_status" :value="scope.row.qrcodeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打印时间" align="center" prop="printTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.printTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="钞箱状态变更时间" align="center" prop="statusChangeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.statusChangeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="在库状态" align="center" prop="inoutStatus" />
      
      <el-table-column label="备注" align="center" prop="remark" /> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改款箱信息对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body draggable>
      <el-form ref="boxInfoRef" :model="form" :rules="rules" label-width="165px">
        <el-form-item label="箱号" prop="boxNo">
          <el-input v-model="form.boxNo" placeholder="请输入箱号" />
        </el-form-item>
        <el-form-item label="款箱类型" prop="boxTypeId">
            <el-input
              :model-value="getBoxTypeName(form.boxTypeId)"
              placeholder="请选择款箱类型"
              readonly
              @click="showBoxTypeSelector"
              clearable
              @clear="() => form.boxTypeId = null"
            >
            <template #append>
              <el-button icon="Search" @click="showBoxTypeSelector" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="所属网点id" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入所属网点id" clearable>
            <template #append>
              <el-button icon="Search" @click="showBankSelector"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="机构id" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入机构id" clearable>
            <template #append>
              <el-button icon="Search" @click="showOrgSelector"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="清分地" prop="clearingPos">
          <el-input v-model="form.clearingPos" placeholder="请输入清分地" clearable>
            <template #append>
              <el-button icon="Search" @click="showClearingPosSelector"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="条码是否打印" prop="barcodeStatus">
          <el-select v-model="form.barcodeStatus" placeholder="请选择条码状态">
            <el-option
              v-for="dict in barcode_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条码(含二维码)是否打印" prop="qrcodeStatus">
          <el-select v-model="form.qrcodeStatus" placeholder="请选择条码">
            <el-option
              v-for="dict in barcode_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    
    <!-- 款箱类型选择对话框 -->
    <el-dialog title="选择款箱类型" v-model="boxTypeOpen" width="40%" append-to-body draggable>
      <BoxType :is-modal="false" :center="false" ref="boxTypeRef" @select="handleBoxTypeSelect" />
      <template #footer>
        <el-button type="primary" @click="handleSelectBoxTypeConfirm">确 定</el-button>
        <el-button @click="boxTypeOpen=false">取 消</el-button>
      </template>
    </el-dialog>
    <el-dialog title="选择网点" v-model="bankOpen" width="40%" append-to-body draggable>
      <Bank :is-modal="false" :center="false" />  
    </el-dialog>
    <el-dialog title="选择机构" v-model="orgOpen" width="40%" append-to-body draggable>
      <Organization :is-modal="false" :center="false" />  
    </el-dialog>
    <el-dialog title="选择清分地" v-model="clearingPosOpen" width="40%" append-to-body draggable>
      <div>清分地选择功能待实现</div>
    </el-dialog>
  </div>
</template>

<script setup name="BoxInfo">
import { listBoxInfo, getBoxInfo, delBoxInfo, addBoxInfo, updateBoxInfo } from "@/api/service/boxInfo"
import BoxType from "@/views/service/boxType/index.vue"
import Bank from "@/views/service/bank/index.vue"
import Organization from "@/views/service/organization/index.vue"

const { proxy } = getCurrentInstance()
const { barcode_status, box_status } = proxy.useDict('barcode_status', 'box_status')

const boxInfoList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeCreateTime = ref([])

// 弹窗相关变量
const boxTypeOpen = ref(false)
const bankOpen = ref(false)
const orgOpen = ref(false)
const clearingPosOpen = ref(false)
const boxTypeRef = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    boxId: null,
    boxNo: null,
    boxTypeId: null,
    bankId: null,
    orgId: null,
    status: null,
    barcodeStatus: null,
    qrcodeStatus: null,
    createTime: null,
  },
  rules: {
    boxNo: [
      { required: true, message: "箱号不能为空", trigger: "blur" }
    ],
    boxTypeId: [
      { required: true, message: "款箱类型不能为空", trigger: "blur" }
    ],
    bankId: [
      { required: true, message: "所属网点id不能为空", trigger: "blur" }
    ],
    clearingPos: [
      { required: true, message: "清分地不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询款箱信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0]
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1]
  }
  listBoxInfo(queryParams.value).then(response => {
    boxInfoList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    boxId: null,
    boxNo: null,
    boxTypeId: null,
    boxBrand: null,
    bankId: null,
    orgId: null,
    clearingPos: null,
    currentPos: null,
    status: null,
    barcodeStatus: null,
    qrcodeStatus: null,
    printTime: null,
    statusChangeTime: null,
    outOfVaultTime: null,
    inBankTime: null,
    outOfBankTime: null,
    inVaultTime: null,
    inoutStatus: null,
    isTransfer: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("boxInfoRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 弹窗相关函数
function showBoxTypeSelector() {
  boxTypeOpen.value = true
}

function showBankSelector() {
  bankOpen.value = true
}

function showOrgSelector() {
  orgOpen.value = true
}

function showClearingPosSelector() {
  clearingPosOpen.value = true
}

/** 根据钞箱类型id获取款箱类型名称 */
function getBoxTypeName(id) {
  if (!id) return ''
  
  // 查找当前列表中是否有该类型
  const type = boxTypeListCache.value.find(item => item.id === id)
  return type ? type.typeName : ''
}

// 缓存款箱类型列表
const boxTypeListCache = ref([])

// 在选择款箱类型时同时缓存类型信息
function handleBoxTypeSelect(row) {
  form.value.boxTypeId = row.id
  // 缓存类型信息
  if (!boxTypeListCache.value.some(item => item.id === row.id)) {
    boxTypeListCache.value.push(row)
  }
  boxTypeOpen.value = false
}

function handleSelectBoxTypeConfirm() {
  boxTypeRef.value.handleSelectConfirm()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加款箱信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getBoxInfo(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改款箱信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["boxInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBoxInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBoxInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除款箱信息编号为"' + _ids + '"的数据项？').then(function() {
    return delBoxInfo(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/boxInfo/export', {
    ...queryParams.value
  }, `boxInfo_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<!-- 全局设置el-input宽度 -->
<style scoped>
:deep(.el-input),
:deep(.el-select .el-input) {
  width: 215px;
  box-sizing: border-box;
}
</style>